import 'package:flutter/material.dart';
import 'package:jd_shop/base/BaseUrl.dart';
import 'package:jd_shop/net/MallmsService.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../services/ScreenAdaper.dart';


class TabGoodsDetail extends StatefulWidget {
  Map arguments;

  TabGoodsDetail({super.key, required this.arguments});

  @override
  _TabGoodsDetailState createState() => _TabGoodsDetailState();
}

class _TabGoodsDetailState extends State<TabGoodsDetail> with AutomaticKeepAliveClientMixin {
  var controller = WebViewController();

  @override
  void initState() {
    super.initState();
    controller.setJavaScriptMode(JavaScriptMode.unrestricted);
    controller.setNavigationDelegate(
      NavigationDelegate(
        onProgress: (int progress) {
          // Update loading bar.
          print(progress);
        },
        onPageStarted: (String url) {},
        onPageFinished: (String url) {},
        onHttpError: (HttpResponseError error) {},
        onWebResourceError: (WebResourceError error) {},
        onNavigationRequest: (NavigationRequest request) {
          if (request.url.startsWith(BaseUrl.baseUrl)) {
            return NavigationDecision.prevent;
          }
          return NavigationDecision.navigate;
        },
      ),
    );
    Object goodsId = this.widget.arguments["goodsId"];
    Object commonId = this.widget.arguments["commonId"];
    controller.loadRequest(Uri.parse(MallmsService.getGoodsDetailH5Url(commonId, goodsId)));
  }

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return WebViewWidget(controller: controller);
  }

  @override
  bool get wantKeepAlive => true;
}
